package com.wnx.action;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.wnx.db.PurchaseDao;
import com.wnx.entity.Menu;
import com.wnx.entity.PurchasePager;
import com.wnx.service.SaleService;

public class PurchaseAction extends ActionSupport {
	private Menu menu;
	private int page=1;
	private String opt;
	private String search;
	
	public String getSearch() {
		return search;
	}
	public void setSearch(String search) {
		this.search = search;
	}
	public String getOpt() {
		return opt;
	}
	public void setOpt(String opt) {
		this.opt = opt;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public Menu getMenu() {
		return menu;
	}
	public void setMenu(Menu menu) {
		this.menu = menu;
	}
	public String execute() throws Exception{
		String opt = this.opt;
		if(opt.equals("all")){
		
			if(page<0){
				page=1;
			}
			
			PurchaseDao sd = new PurchaseDao();
			PurchasePager p = sd.getPurchaseByPager(page-1);
			Map<String, Object> session = ActionContext.getContext().getSession();
			session.put("opt", opt);
			session.put("p", p);
			return SUCCESS;
		}else if(opt.equals("cls")){
			HttpServletRequest request = ServletActionContext.getRequest();
			int mid = Integer.parseInt(request.getParameter("mid"));
			if(page<0){
				page=1;
			}
			
			PurchaseDao sd = new PurchaseDao();
			PurchasePager p = sd.getPurchaseByPagerBymid(page-1, mid);
			Map<String, Object> session = ActionContext.getContext().getSession();
			session.put("p", p);;
			session.put("opt", opt);
			return SUCCESS;
		}else if(opt.equals("search")){
			if(page<0){
				page=1;
			}
			
			PurchaseDao sd = new PurchaseDao();
			PurchasePager p = sd.getPurchaseByPagerSearch(page-1, this.search);
			Map<String, Object> session = ActionContext.getContext().getSession();
			session.put("p", p);
			session.put("opt", opt);
			return SUCCESS;
		}else {
			
			return SUCCESS;
		}
	}
}
